/**
 * 安卓白金英雄坛制作组
 * <br>
 * 文件名：GmudMap.java
 * <br>
 * 创建时间：2013-5-21 下午12:41:17
 * <br>
 * 所属项目：GmudEX
 * <br>
 *
 * @author 12548
 *         <br>
 */
package lostland.gmud.exv2.data;

import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.LoadingScreen;
import lostland.gmud.exv2.MapScreen;
import lostland.gmud.exv2.MapTile;
import android.graphics.Canvas;
import android.graphics.Point;

/**
 * 类名：GmudMap <br>
 * 说明：地图（显示）
 *
 * @author 12548
 */
public class GmudMap {

	public static final int MP_WALKABLE = -1;
	public static final int MP_UNWALKABLE = 0;
	public static final int MP_CHANGETO = -1;// 不要管他……
	public static final int MP_NPC = 2;
	public static final int MP_EVENT = 3;
	public static final int MP_SIMPLESCRIPT = 4;


	private static String MAP_NAME[];


	public MapExtraInfo extraInfo = null;

	Point p[][][];

	public int width, height;
	public int id;

	/**
	 * 获取地图id。
	 *
	 * @return 地图的id
	 */
	public int getId() {
		return id;
	}

	public GmudMap(int width, int height, int id) {
		int i, j, k;
		p = new Point[4][width][height];
		for (i = 0; i < 4; i++) {
			for (j = 0; j < width; j++) {
				for (k = 0; k < height; k++) {
					p[i][j][k] = new Point(-1, -1);
				}
			}
		}
		this.width = width;
		this.height = height;
		this.id = id;
	}

	public void setPoint(int z, int x, int y, int tileX, int tileY) {
		p[z][x][y].x = tileX;
		p[z][x][y].y = tileY;
	}

	public int getTileX(int z, int x, int y) {
		if (x < 0 || x >= width || y < 0 || y >= height)
			return 1;
		return p[z][x][y].x;
	}

	public int getTileY(int z, int x, int y) {
		if (x < 0 || x >= width || y < 0 || y >= height)
			return 0;
		return p[z][x][y].y;
	}

	public void setWalkable(int x, int y, int walkable) {
//		Logger.d("Walkable set to %d on %d (%d, %d)", walkable, this.id, x, y);
		p[3][x][y].x = walkable;
	}

	public static void clearEvent(FlyPoint place) {
		if(place == null)
			return;
		Gmud.maps[place.map].setWalkable(place.x, place.y, MP_WALKABLE);
		Gmud.maps[place.map].setEvent(place.x, place.y, -1);
	}

	public void setEvent(int x, int y, int eventid) {
		p[3][x][y].y = eventid;
	}

	public int getWalkable(int x, int y) {
		if (x < 0 || x >= width || y < 0 || y >= height)
			return 0;
		return p[3][x][y].x;
	}

	public int getEvent(int x, int y) {
		if (x < 0 || x >= width || y < 0 || y >= height)
			return 0;
		return p[3][x][y].y;
	}

	public boolean isWalkable(int x, int y) {
		if ((x < 0) || (x >= width) || (y < 0) || (y >= height))
			return false;

		return p[3][x][y].x == MP_WALKABLE;
	}


	public GmudMap npc(int x, int y, int npcid) {
		if (Game.getData().vanish_note.contains(npcid)) {
			setWalkable(x, y, MP_WALKABLE);
			setEvent(x, y, -1);
		} else {
			setWalkable(x, y, MP_NPC);
			setEvent(x, y, npcid);
		}
		return this;
	}

	public GmudMap event(int x, int y, int eventid) {
		setWalkable(x, y, MP_EVENT);
		setEvent(x, y, eventid);
		return this;
	}

	public GmudMap changeto(int x, int y, FlyPoint target) {
		setWalkable(x, y, MP_CHANGETO);
		setEvent(x, y, target.map * 1000000 + target.x * 1000 + target.y);
// MainCharTile.currentDirection = target.d;
		return this;
	}

	public void draw(MapTile tile, int mapX, int mapY) {
		Game.getInstance().getGraphics().clear(Gmud.getBgColor());

		int j, k;

		Game.getInstance().drawMap = true;
		Game.getInstance().mapX = mapX;
		Game.getInstance().mapY = mapY;
		Game.getInstance().mapDX = 0;
		Game.getInstance().mapDY = 0;

		for (int i = 0; i < 3; i++) {
			for (j = 0; j < MapScreen.C_ROWS; j++) {
				for (k = 0; k < MapScreen.C_COLUMNS; k++)
					if (getTileX(i, j + mapX, k + mapY) > -1
							&& getTileY(i, j + mapX, k + mapY) > -1) {
						// Log.v("drawMap:","drawing:screen("+j+","+k+")is tile("+badman_map.getTileX(i, j+mapX,
// k+mapY)+","+badman_map.getTileY(i, j+mapX, k+mapY)+")in layer"+i);
						tile.drawInMap(j, k, getTileX(i, j + mapX, k + mapY),
								getTileY(i, j + mapX, k + mapY));
					}
			}
		}
		for (j = 0; j < MapScreen.C_ROWS; j++) {
			for (k = 0; k < MapScreen.C_COLUMNS; k++)
				if (getWalkable(j + mapX, k + mapY) == GmudMap.MP_NPC) {
					final Npc npc = Gmud.getNpc(getEvent(j + mapX, k + mapY));
					if(npc.haveFlag(Npc.FLAG_DISAPPEAR_AFTER_DIE) && !npc.alive()) {
						setWalkable(j + mapX, k + mapY, MP_WALKABLE);
						setEvent(j + mapX, k + mapY, -1);
					}
					else {
						npc.drawInMap(j, k);
					}
				}
		}
	}

	public void drawInCanvas(MapTile tile, int mapX, int mapY, Canvas canvas) {
		Game.getInstance().getGraphics().clear(Gmud.getBgColor());

		int j, k;

		Game.getInstance().drawMap = true;
		Game.getInstance().mapX = mapX;
		Game.getInstance().mapY = mapY;
		Game.getInstance().mapDX = 0;
		Game.getInstance().mapDY = 0;

		for (int i = 0; i < 3; i++) {
			for (j = 0; j < MapScreen.C_ROWS; j++) {
				for (k = 0; k < MapScreen.C_COLUMNS; k++)
					if (getTileX(i, j + mapX, k + mapY) > -1
							&& getTileY(i, j + mapX, k + mapY) > -1) {
						// Log.v("drawMap:","drawing:screen("+j+","+k+")is tile("+badman_map.getTileX(i, j+mapX,
// k+mapY)+","+badman_map.getTileY(i, j+mapX, k+mapY)+")in layer"+i);
						tile.drawInCanvas(j, k, getTileX(i, j + mapX, k + mapY),
								getTileY(i, j + mapX, k + mapY), 0, 0, canvas);
					}
			}
		}
		for (j = 0; j < MapScreen.C_ROWS; j++) {
			for (k = 0; k < MapScreen.C_COLUMNS; k++)
				if (getWalkable(j + mapX, k + mapY) == GmudMap.MP_NPC) {
					final Npc npc = Gmud.getNpc(getEvent(j + mapX, k + mapY));
					if(npc.haveFlag(Npc.FLAG_DISAPPEAR_AFTER_DIE) && !npc.alive()) {
						setWalkable(j + mapX, k + mapY, MP_WALKABLE);
						setEvent(j + mapX, k + mapY, -1);
					}
					else {
						npc.drawInMap(j, k);
					}
				}
		}
	}

	public void drawWalkingInCanvas(MapTile tile, int x, int y, int dx, int dy, Canvas canvas) {
		Game.getInstance().getGraphics().clear(Gmud.getBgColor());

		Game.getInstance().drawMap = true;
		Game.getInstance().mapDX = dx;
		Game.getInstance().mapDY = dy;
		Game.getInstance().mapX = x;
		Game.getInstance().mapY = y;

		int j, k;
		for (int i = 0; i < 3; i++) {
			for (j = -1; j <= MapScreen.C_ROWS; j++) {
				for (k = -1; k <= MapScreen.C_COLUMNS; k++)
					if (getTileX(i, j + x, k + y) > -1 && getTileY(i, j + x, k + y) > -1) {
						tile.drawInCanvas(j, k, getTileX(i, j + x, k + y), getTileY(i, j + x, k + y),
								dx, dy, canvas);
					}
			}
		}
		for (j = -1; j <= MapScreen.C_ROWS; j++) {
			for (k = -1; k <= MapScreen.C_COLUMNS; k++)
				if (getWalkable(j + x, k + y) == GmudMap.MP_NPC) {
					final Npc npc = Gmud.getNpc(getEvent(j + x, k + y));
					if(npc.haveFlag(Npc.FLAG_DISAPPEAR_AFTER_DIE) && !npc.alive()) {
						setWalkable(j + x, k + y, MP_WALKABLE);
						setEvent(j + x, k + y, -1);
					}
					else {
						npc.drawInMap(j, k, dx, dy);
					}
				}
		}
	}


	public void drawWalking(MapTile tile, int x, int y, int dx, int dy) {
		Game.getInstance().getGraphics().clear(Gmud.getBgColor());

		Game.getInstance().drawMap = true;
		Game.getInstance().mapDX = dx;
		Game.getInstance().mapDY = dy;
		Game.getInstance().mapX = x;
		Game.getInstance().mapY = y;

		int j, k;
		for (int i = 0; i < 3; i++) {
			for (j = -1; j <= MapScreen.C_ROWS; j++) {
				for (k = -1; k <= MapScreen.C_COLUMNS; k++)
					if (getTileX(i, j + x, k + y) > -1 && getTileY(i, j + x, k + y) > -1) {
						tile.drawInMap(j, k, getTileX(i, j + x, k + y), getTileY(i, j + x, k + y),
								dx, dy);
					}
			}
		}
		for (j = -1; j <= MapScreen.C_ROWS; j++) {
			for (k = -1; k <= MapScreen.C_COLUMNS; k++)
				if (getWalkable(j + x, k + y) == GmudMap.MP_NPC) {
					final Npc npc = Gmud.getNpc(getEvent(j + x, k + y));
					if(npc.haveFlag(Npc.FLAG_DISAPPEAR_AFTER_DIE) && !npc.alive()) {
						setWalkable(j + x, k + y, MP_WALKABLE);
						setEvent(j + x, k + y, -1);
					}
					else {
						npc.drawInMap(j, k, dx, dy);
					}
				}
		}
	}

	public static int getMapCount() {
		if (MAP_NAME == null) {
			MAP_NAME = LoadingScreen.loadMapnameJson();
		}
		return MAP_NAME.length;
	}

	public String getName() {
		if (MAP_NAME == null) {
			MAP_NAME = LoadingScreen.loadMapnameJson();
		}

		return MAP_NAME[id];
	}


}
